<template>
  <div class="app_main" style="position: relative">
    <div class="search_box">
      <el-form ref="searchForm" :inline="true" :model="searchForm" size="mini">
        <search-form-box :show-more-btn="false">
          <template slot="showSearch">
            <el-form-item label="日期" prop="startDate">
              <el-date-picker
                v-model="searchForm.startDate"
                type="date"
                placeholder="选择开始日期"
                value-format="yyyy-MM-dd"
                format="yyyy年MM月dd日"
              >
              </el-date-picker>
            </el-form-item>
            <el-form-item label="至" prop="endDate">
              <el-date-picker
                v-model="searchForm.endDate"
                type="date"
                placeholder="选择结束日期"
                value-format="yyyy-MM-dd"
                format="yyyy年MM月dd日"
              >
              </el-date-picker>
            </el-form-item>
            <el-form-item label="工人" prop="worker_id">
              <el-select v-model="searchForm.worker_id" clearable filterable>
                <el-option v-for="item in workerArr" :key="item.id" :value="item.id" :label="item.name"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="资源类型" prop="media_type_id">
              <el-select v-model="searchForm.media_type_id" clearable filterable>
                <el-option v-for="item in mediaTypeArr" :key="item.id" :value="item.id" :label="item.name"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="工单类型" prop="work_order_type_id">
              <el-select v-model="searchForm.work_order_type_id" clearable filterable>
                <el-option value="1" label="新安装"></el-option>
                <el-option value="2" label="上刊"></el-option>
                <el-option value="3" label="下刊"></el-option>
                <!--                <el-option value="4" label="巡检"></el-option>-->
                <el-option value="5" label="维修"></el-option>
<!--                <el-option value="6" label="维修申报"></el-option>-->
                <el-option value="7" label="换画"></el-option>
                <el-option value="8" label="中间监播"></el-option>
              </el-select>
            </el-form-item>
          </template>
          <template slot="searchBtn">
            <el-form-item>
              <el-button size="mini" type="primary" plain @click="search">查询</el-button>
              <el-button size="mini" type="info" plain @click="resetForm('searchForm')">重置</el-button>
            </el-form-item>
          </template>
        </search-form-box>
      </el-form>
    </div>
    <div style="position: absolute;top: 30px;;right: 20px">
      <el-popover
        placement="bottom"
        width="700"
        trigger="hover"
      >
        <el-table :data="tipData" border width="700">
          <el-table-column label="数量统计规则（针对一天内的提交）" header-align="center">
            <el-table-column width="150" property="name" label="资源类型" align="center" show-overflow-tooltip
            ></el-table-column>
            <el-table-column width="570" property="rule" label="规则" align="center" show-overflow-tooltip
            ></el-table-column>
          </el-table-column>
        </el-table>
        <el-button slot="reference" size="mini" icon="el-icon-warning-outline">说明</el-button>
      </el-popover>
    </div>
    <div class="data_box">
      <el-table v-loading="tableLoading" ref="dataTable" :data="tableData" show-summary border
                size="mini" height="calc(100vh - 170px)"
      >
        <el-table-column fixed="left" width="50" label="序号" align="center">
          <template slot-scope="scope">
            <span>{{ (listParams.page - 1) * listParams.page_size + scope.$index + 1 }}</span>
          </template>
        </el-table-column>
        <el-table-column label="日期" width="120" prop="date" align="center"></el-table-column>
        <el-table-column label="工人名称" min-width="120" prop="worker_name" align="center"></el-table-column>
        <el-table-column label="资源类型" min-width="200" show-overflow-tooltip align="center">
          <template slot-scope="scope">
            {{ getMediaTypeName(scope.row.media_type_id) }}
          </template>
        </el-table-column>
        <el-table-column label="动作" width="100" prop="work_order_type_name" align="center"></el-table-column>
        <el-table-column label="通过数量" width="100" prop="pass_num" align="center"></el-table-column>
        <el-table-column label="未通过数量" width="100" prop="no_pass_num" align="center"></el-table-column>
      </el-table>
    </div>
    <!-- <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page.sync="searchForm.page"
      :page-sizes="[10, 20, 50, 100, 200, 400]"
      :page-size.sync="searchForm.page_size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      style="margin-top: 10px"
    >
    </el-pagination> -->
  </div>
</template>

<script>
import { getFirstDayOfMonth, formatDate } from '@/utils/util.js'
import SearchFormBox from '@/components/SearchFormBox'
import { installCountPage } from '@/api/workOrder/workSubmit'
import { listWorker } from '@/api/workOrder/workOrder'
import { get_media_types } from '@/api/building/building'

export default {
  name: 'page',
  components: { SearchFormBox },
  data() {
    return {
      tableData: [],
      tableLoading: false,
      searchForm: {
        page: 1,
        page_size: 10,
        // 开始时间
        startDate: getFirstDayOfMonth(),
        // 结束时间
        endDate: formatDate(new Date()),
        // 工人id
        worker_id: null,
        // 工单类型ID
        work_order_type_id: null,
        // 媒体类型ID
        media_type_id: null
      },
      listParams: {
        page: 1,
        page_size: 10,
        // 开始时间
        startDate: getFirstDayOfMonth(),
        // 结束时间
        endDate: formatDate(new Date()),
        // 工人id
        worker_id: null,
        // 工单类型ID
        work_order_type_id: null,
        // 媒体类型ID
        media_type_id: null
      },
      total: null,
      workerArr: [],
      mediaTypeArr: [],
      tipData: [
        {
          name: '社区智能框',
          rule: '以实际上画数量为准，①纯上刊1幅为1幅，②同点位先下后上（上刊）：上刊1幅、下刊1幅只计1幅，③纯下刊1幅为1幅'
        },
        {
          name: '楼宇',
          rule: '以实际上画数量为准，①纯上刊1幅为1幅，②同点位先下后上（上刊）：上刊1幅、下刊1幅只计1幅，③纯下刊1幅为1幅'
        },
        {
          name: '路名牌',
          rule: '以实际上画数量为准，①纯上刊1幅为1幅，②同点位先下后上（上刊）：上刊1幅、下刊1幅只计1幅，③纯下刊1幅为1幅'
        },
        {
          name: '综合市场',
          rule: '只计综合市场数量（例如：湟里市场上刊2幅，计数为1）'
        },
        {
          name: '加油站',
          rule: '只计加油站数量（例如：恐龙园加油站上刊3幅，计数为1）'
        },
        {
          name: '九洲软膜',
          rule: '以实际上画数量为准，①纯上刊1幅为1幅，②同点位先下后上（上刊）：上刊1幅、下刊1幅只计1幅，③纯下刊1幅为1幅'
        },
        {
          name: '家家爱大牌',
          rule: '以实际上画数量为准，①纯上刊1幅为1幅，②同点位先下后上（上刊）：上刊1幅、下刊1幅只计1幅，③纯下刊1幅为1幅'
        },
        {
          name: '大屏',
          rule: '只计大屏数量（例如：九洲新世界大屏上刊2个客户，计数为1）'
        }
      ]
    }
  },
  created() {
    this.initWorkerArr()
    this.initMediaTypeArr()
    this.getList()
  },
  updated () {
	  this.$nextTick(() => {
	      this.$refs['dataTable'].doLayout();
	  })
  },
  methods: {
    handleSizeChange(val) {
      this.listParams.page_size = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listParams.page = val
      this.getList()
    },
	validDate(){
		if(!this.listParams.startDate){
			this.$message({message: '请选择开始日期',type: 'warning'});
			return false;
		}
		if(!this.listParams.endDate){
			this.$message({message: '请选择结束日期',type: 'warning'});
			return false;
		}
		return true;
	},
    getList() {
		if(!this.validDate()){
			return false;
		}
      this.tableLoading = true
      installCountPage(this.listParams).then(res => {
        this.tableLoading = false
        this.total = res.data.total
        this.tableData = res.data.records
      }).catch(() => {
        this.tableLoading = false
      })
    },
    search() {
      this.searchForm.page = 1
      this.listParams = Object.assign({}, this.searchForm)
      this.getList()
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
      this.listParams = Object.assign({}, this.searchForm)
      this.getList()
    },
    initWorkerArr() {
      listWorker().then(res => {
        this.workerArr = res.data
      })
    },
    initMediaTypeArr() {
      get_media_types().then(res => {
        this.mediaTypeArr = res.data
      })
    },
    getMediaTypeName(id) {
      let find = this.mediaTypeArr.find(item => item.id === id)
      if (find) {
        return find.name
      } else {
        return ''
      }
    }
  }
}
</script>

<style scoped lang="scss">
.app_main {
  padding: 20px 20px 0 20px;
}
</style>
